package com.nativetalk.service.four.label;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtTeacherLabel;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtTeacherLabelDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 2016/11/1.
 */
@Service
public class FourLabelServiceImpl implements FourLabelService {
    private static Logger logger = Logger.getLogger(FourLabelServiceImpl.class);
    @Resource
    private TdNtTeacherLabelDao tdNtTeacherLabelDao;
    @Override
    public RetInfo findTeacherLabel(int tea_id) {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            /**
             *
             */
            Map<String , Object> map = new HashMap<>();

            map.put("systemLabel", MemcachedUtils.get(MemcachedKey.systemLabel));
            map.put("labels", tdNtTeacherLabelDao.findTdNtTeacherListTeaIdEn(tea_id));
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo add(TdNtTeacherLabel tdNtTeacherLabel) {
        String logInfo = this.getClass().getName() + ":add:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            // 查询laben对应的数据字典ID（dict_id）
            Integer dict_id = tdNtTeacherLabelDao.findDictIdByLabelEnName(tdNtTeacherLabel.getLab_en_name());
            if (dict_id != null) {
                tdNtTeacherLabel.setDict_id(dict_id);
            }

            tdNtTeacherLabelDao.insert(tdNtTeacherLabel);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo udpate(TdNtTeacherLabel tdNtTeacherLabel) {
        String logInfo = this.getClass().getName() + ":udpate:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            // 查询laben对应的数据字典ID（dict_id）
            Integer dict_id = tdNtTeacherLabelDao.findDictIdByLabelEnName(tdNtTeacherLabel.getLab_en_name());
            if (dict_id != null) {
                tdNtTeacherLabel.setDict_id(dict_id);
            }

            tdNtTeacherLabelDao.update(tdNtTeacherLabel);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo delete(TdNtTeacherLabel tdNtTeacherLabel) {
        String logInfo = this.getClass().getName() + ":delete:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtTeacherLabelDao.delete(tdNtTeacherLabel);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
